/**
 * Created by mark on 2018/4/10.
 * 用户模块--找回密码
 */


require("./user-password-reset.css");
require("page/common/nav-simple/nav-simple.js");
var _user = require("service/user-service.js");
var _mm = require("util/mm.js");

var  formError = {
    show : function(errMsg){
        $(".error-item").show().find(".err-msg").text(errMsg);
    },
    hide : function(){
        $(".error-item").hide().find(".err-msg").text("");
    }
}

var page = {
    data : {
        username : "",
        question : "",
        answer : "",
        token : "",
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadStepUsername();
    },
    bindEvent : function() {
        var _this = this;
        $("#submit-username").click(function () {
            _this.submitUsername();
        });
        $("#submit-question").click(function () {
            _this.submitQuestion();
        });
        $("#submit-password").click(function(){
            _this.submitPassword();
        });

    },
    //第一步：输入用户名点击提交按钮的事件
       submitUsername : function(){
        var _this = this;
        var username = $.trim($("#username").val());
        if(username){//用户名不为空
            _user.getQuestion(username,
                function(res){
                    _this.data.username = username;
                    _this.data.question = res.msg;
                    _this.loadStepQuestion();
                },
                function(err){
                    formError.show(err.msg)
                }
            );
        }else{//用户名为空
            formError.show("用户名不能为空！")
        }
    },
    //第二步:输入回答问题后提交按钮的事件
    submitQuestion : function(){
        var _this = this;
        var answer = $.trim($("#answer").val());
        if(answer){//回答问题的答案为不为空
            _user.checkAnswer({
                username : _this.data.username,
                question : _this.data.question,
                answer : answer,
                }, function(res){
                    _this.data.answer = answer;
                    _this.data.token = res.data;
                    _this.loadStepPassword();
                }, function(err){
                    formError.show(err.msg);
                }
            );
        }else{//回答问题的答案为为空
            formError.show("填写的答案结果不能为空！")
        }
    },
    //第三步:填写新密码后提交按钮的事件
    submitPassword : function(){
        var _this = this;
        var password = $.trim($("#password").val());
        var passwordConfirm = $.trim($("#password-confirm").val());
        if(!password || password.length < 6){
            formError.show("密码不能小于6位数");
            return;
        }
        if(password !== passwordConfirm){
            formError.show("两次输入的密码不一致！");
            return;
        }
        _user.resetPassword({
                    username : _this.data.username,
                    passwordNew : password,
                    forgetToken : _this.data.token,
                }, function(res){
                   window.location.href = "./result.html?type=password-reset";
                }, function(err){
                    formError.show(err.msg);
                }
            );
    },
    //加载输入用户名的第一步
    loadStepUsername : function(){
        $(".step-username").show();
    },
    //加载输入密码提示问题答案的一步
    loadStepQuestion : function(){
        formError.hide();
        $(".step-username").hide().siblings(".step-question").show()
            .find(".question").text(this.data.question);
    },
    loadStepPassword : function(){
        formError.hide();
        $(".step-question").hide().siblings(".step-password").show();
    }
};
$(function(){
    page.init();
});
